package com.spring.controller;

import com.base.Result;
import com.base.ResultCode;
import com.spring.controller.asyncthread.MyAsyncThread;
import com.spring.controller.service.HelloServiceRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class HelloController extends BaseController {

    @Autowired
    private MyAsyncThread asyncThread;

	@RequestMapping
    public String helloWorld(Model model){  
        model.addAttribute("message","Hello World!!!");  
        return "HelloWorld";  
    }


    /**
     * 异步方法执行测试（Controller中异步的又一种实现方式）
     * @return
     */
    @RequestMapping
    @ResponseBody
    public ApiModel testAsync() {
	    asyncThread.run();
        System.out.print(new Date());
        System.out.println("可能正在执行run....." );
	    return buildModel("执行完返回结果了...");
    }

    @Autowired
    private HelloServiceRemote helloServiceRemote;

    /**
     * service中异步的实现方式
     * @param name
     * @return
     */
    @RequestMapping
    @ResponseBody
    public ApiModel testAsync2(String name) {
        helloServiceRemote.sayHello(name);
        return buildModel("执行完返回结果了...");
    }


    /**
     * 美观美化的API接口返回
     * @param id
     * @return
     */
    @RequestMapping
    @ResponseBody
    public Result testResult(Integer id) {
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        return Result.success(new Date());
    }


}
